package com.heima.wemedia.builder;

import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import org.springframework.beans.BeanUtils;

import java.util.Date;

public class ArticleDtoBuilder {

    private WmNews wmNews;
    private WmUserMapper wmUserMapper;
    private WmChannelMapper wmChannelMapper;

    private ArticleDto articleDto = new ArticleDto();

    public ArticleDtoBuilder(WmNews wmNews, WmUserMapper wmUserMapper, WmChannelMapper wmChannelMapper){
        this.wmNews = wmNews;
        this.wmUserMapper = wmUserMapper;
        this.wmChannelMapper = wmChannelMapper;
    }

    /**
     * 构建bean的属性拷贝
     * @return
     */
    public ArticleDtoBuilder buildBeanCopy(){
        BeanUtils.copyProperties(wmNews,articleDto);
        return this;
    }

    /**
     * 构建bean的基本属性
     * @return
     */
    public ArticleDtoBuilder buildBasic(){
        //布局
        articleDto.setLayout(wmNews.getType());
        //创建时间
        articleDto.setCreatedTime(new Date());
        articleDto.setLikes(0);
        articleDto.setComment(0);
        articleDto.setCollection(0);
        articleDto.setViews(0);


        //当这个文章之前审核通过 过，意味着当前自媒体文章表中就有article_id（有值）
        if(wmNews.getArticleId() != null){
            articleDto.setId(wmNews.getArticleId());
        }
        return this;
    }

    /**
     * 构建bean的作者信息
     * @return
     */
    public ArticleDtoBuilder buildAuthor(){
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        if(null != wmUser){
            articleDto.setAuthorId(wmUser.getId().longValue());
            articleDto.setAuthorName(wmUser.getName());
        }
        return this;
    }

    /**
     * 构建bean的频道信息
     * @return
     */
    public ArticleDtoBuilder buildChannel(){
        WmChannel wmChannel = wmChannelMapper.selectById(wmNews.getChannelId());
        if(null != wmChannel){
            articleDto.setChannelName(wmChannel.getName());
        }
        return this;
    }

    public ArticleDto builder(){
        return articleDto;
    }
}
